var fileSystemObject = new ActiveXObject("Scripting.FileSystemObject");
var adoStream;

//获取XmlHttpRequest
function getXMLHttpRequest(){
	var XMLHttpRequest = null;
	if (typeof ActiveXObject != "undefined") {
		var aVersions = ["Microsoft.XMLHTTP", "Msxml2.XMLHttp.6.0", "Msxml2.XMLHttp.5.0", "Msxml2.XMLHttp.4.0", "Msxml2.XMLHttp.3.0"];
		for (var i = 0; i < aVersions.length; i++) {
			try {XMLHttpRequest = new ActiveXObject(aVersions[i]);}catch (ex) {}//注释：不能alert循环的创建的
		}
	} else if (typeof XMLHttpRequest != "undefined"){
		XMLHttpRequest = new XMLHttpRequest();
	}
			
		return XMLHttpRequest;
}


var Microsoft_Scan = {
	// 扫描信息图片路径数据存放集合
	scanImgArr : [],
	scanImgPath:"",
	scanOpenStatus:false,
	// 开启扫描仪
	openScaner : function() {
		//扫描类型
		var scanType=$("#ScanerSelect").val();
		var scaner = ocx;
		//扫描仪6240s和平板XT3500的DSMode为9
		if(scanType=='Microtek_6240s' || scanType=='Microtek_XT3500'){
			scaner.DSMode(9);
		//告诉扫描仪S8090的DSMode为8
		}else if(scanType=='Microtek_S8090'){
			scaner.DSMode(8);
		}else{
			scaner.DSMode(0);
		}
		//Long IsScannerOpen()
		//说明：
		//返回值
		//1：扫描仪已打开。
		//0：扫描仪未打开。
		
		var openStatus = scaner.openScanner();
		if (openStatus != 1) {
			var errorCode = scaner.GetErrorCode();
			this.scanOpenStatus=false;
			alert("扫描仪开启失败!错误代码:" + errorCode);
		}else{
			this.scanOpenStatus=true;
		}
	
		
		scaner.SetImageName(this.tempImgFile, "0", 1, 7);
		// 去钉孔
		scaner.PunchHoleRemoval(1);
		// 自动纠偏
		scaner.AutoDeskew(1);
		// 黑边补白
		scaner.BlackBorderRemoval(1);// 注释：默认黑边补白
		// 文件格式,可以为下面的几个值:
		// 0:bmp
		// 1:jpg
		// 2.单页tif
		// 3.多页tif
		// 4.单页pdf
		// 5.多页pdf
		// 6.可检索的单页pdf，需要支持OCR识别。
		// 7.可检索的多页pdf，需要支持OCR识别。
		scaner.ImageFormat = 1;
		// 扫描仪类型
		// 0 Flatbed 平板扫描仪
		// 1 ADF
		// 2 ADF duplex
		var scanType=$("#ScanerSelect").val();
		// 扫描仪类型
		// 0 Flatbed 平板扫描仪
		// 1 ADF
		// 2 ADF duplex
		if(scanType=='Microtek_6240s' || scanType=='Microtek_S8090' ){
			scaner.ScanSourceType = 1;
		//0 Flatbed 平板扫描仪	
		}else if(scanType=='Microtek_XT3500'){
			scaner.ScanSourceType = 0;
		}else{
			scaner.ScanSourceType = 1;
		}
		
	},
	// 扫描配置
	init : function() {
		var scaner = ocx;
		
		
	},
	// 关闭扫描仪
	closeScaner : function() {
		var scaner = ocx;
		scaner.CancelXfers();
		var closeStatus = scaner.CloseScanner();
		if (closeStatus != 1) {
			var errorCode = scaner.GetErrorCode();
			alert("扫描仪关闭失败!错误代码:" + errorCode);
		}else{
			alert("扫描仪关闭成功!");
		}
	},
	// 打开UI界面设置扫描仪
	settingScaner : function() {
		var scaner = ocx;
		var scanType=$("#ScanerSelect").val();
		//扫描仪6240s和平板XT3500的DSMode为9
		if(scanType=='Microtek_6240s' || scanType=='Microtek_XT3500'){
			scaner.DSMode(9);
		//告诉扫描仪S8090的DSMode为8
		}else if(scanType=='Microtek_S8090'){
			scaner.DSMode(8);
		}else{
			scaner.DSMode(0);
		}
		var openStatus = scaner.openScanner();
		scaner.ShowUISettings();
	},
	// 单扫
	startScan : function(scanType) {
		var scaner = ocx;
		var scanStatus=0;
		// 单扫
		if (scanType == 1) {
			scanStatus=scaner.Scan(1, 0);
		//连扫
		} else {
			scanStatus=scaner.Scan(-1, 0);
		}
		return scanStatus;

	},
	// 取消扫描
	cancleScan : function() {
		var scaner = ocx;
		scaner.CancelXfers();
	},
	//上传本地扫描图片到服务器
	uploadLocalImg:function(bSuccess){
		var scaner = ocx;
		if (bSuccess == 1){
			//获取本地扫描图片路径
			var imagePath = scaner.GetCurrentScanImagePath();
			//alert("扫描图片路径:"+imagePath);
			var result = uploadPhotoImg(imagePath);
			if(result.ret=="success"){
				this.scanImgArr.push(result.fileName);
			}
		}else{
			scaner.CancelXfers();
			alert("错误代码："+ScanOcx.GetErrorCode()+"\r\n此页文件扫描未成功，请重新扫描！");
		}
	},
	// 扫描临时文件生成路径
	tempImgFile : "D:\\MrLocal",
	// 删除扫描生成临时文件夹内容
	deleteTempImgs : function() {
		var fileSystemObject = new ActiveXObject("Scripting.FileSystemObject");
		var fileSystem = fileSystemObject.GetFolder(this.tempImgFile);
		var enumFiles = new Enumerator(fileSystem.files);
		for (; !enumFiles.atEnd(); enumFiles.moveNext()) {
			enumFiles.item().Delete();
		}
	}
}

//扫描
function scan(scanType){
	//1.删除上次扫描生成的临时文件
	Microsoft_Scan.deleteTempImgs();
	//清空存放扫描信息数组
	Microsoft_Scan.scanImgArr=[];
	//2.开启扫描仪
	if(!Microsoft_Scan.scanOpenStatus){
		Microsoft_Scan.openScaner();
	}
	
	//3.配置扫描仪
	//Microsoft_Scan.init();
	//4.开始扫描
	var scanStatus = Microsoft_Scan.startScan(scanType);
	//扫描成功为1
	if(scanStatus==1){
		if(Microsoft_Scan.scanImgArr.length>0){
			var basePath=$("#basePath").val();
			var firstGuid=$("#firstGuid").val();
			console.log(Microsoft_Scan.scanImgArr.toString());

		  
			$.ajax({
				type:"post",
				url:basePath+"/scan/uploadScanImgs",
				data:{firstGuid:firstGuid,scanImageList:Microsoft_Scan.scanImgArr.toString()},
				success:function(data){
					Microsoft_Scan.scanImgArr=[];
					 if(data.success){

						window.location.href=basePath+"/scan/searchScan?firstGuid="+firstGuid;
					 }else{
						 alert(data.msg);
					 }
				}
			});
			Microsoft_Scan.scanImgArr=[];
			
		}else{
			Microsoft_Scan.scanImgArr=[];
			alert("扫描数据为空,请重新扫描!");
		}
		
	}else{
		alert("扫描失败!错误代码:+"+ocx.GetErrorCode());
	}
}



//将临时文件上传至服务器
function uploadPhotoImg(imagePath){
	var basePath=$("#basePath").val();
	var firstGuid=$("#firstGuid").val();
	//var fileName = imagePath
			
	adoStream = new ActiveXObject("ADODB.Stream");
	adoStream.Mode = 3;//read write
			
	adoStream.Open();
	adoStream.Type = 1;
		   	
	var fileType = imagePath.substring(imagePath.lastIndexOf(".")+1);
	var upOneURL = basePath+"/upload/uploadLocalImg";
	adohttp = getXMLHttpRequest();
	adohttp.open('post', upOneURL, false);
	adohttp.setRequestHeader('-fileType',fileType);
	adohttp.setRequestHeader('-firstGuid',firstGuid);//Scan.MrId
			
			
	adohttp.setRequestHeader('-pageOneLocalPath',imagePath.replace(/\\/g,"/"));
	adohttp.setRequestHeader('Content-Type', 'application/x- www-form-urlencoded');

		   	
	adoStream.LoadFromFile(imagePath);
			
			
	var fileSize = fileSystemObject.GetFile(imagePath).Size;
	var adoStreamSize = adoStream.Size;
			
	if (fileSize != adoStreamSize || fileSize < 3584 || adoStreamSize < 3584){
		adoStream.Close();////
		adoStream = null;
		adohttp = null;
		return "noUp";
	}
			
	adohttp.setRequestHeader("Content-Length",adoStream.Size);
			
	adohttp.send(adoStream.Read(adoStream.Size));
	//返回数据
	var result = JSON.parse(adohttp.responseText);
		   
		   	
	adoStream.Close();////
	adoStream = null;
		   	
	adohttp = null;
		   	
	return result;
}


